package servlet;

import domain.NingboGrowthRate;
import org.json.JSONArray;
import org.json.JSONObject;
import service.NingboGrowthRateService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class NingboGrowthRateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        NingboGrowthRateService nbgrs = new NingboGrowthRateService();
        request.setCharacterEncoding("UTF-8");
        List<NingboGrowthRate> list = nbgrs.selectDataByCounty(request.getParameter("address"));
        JSONArray jsonArray = new JSONArray();
        for(NingboGrowthRate nbgr : list){
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("type", nbgr.getType());
            jsonObj.put("rate", nbgr.getRate());
            jsonArray.put(jsonObj);
        }
        response.setHeader("content-type","text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(jsonArray.toString());
    }
}
